package org.lanqiao.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.lanqiao.orm.EleInfo;
import org.lanqiao.orm.EleMind;
import org.lanqiao.orm.EleOpeartor;
import org.lanqiao.service.ICheckLogin;
import org.lanqiao.service.IManagerFun;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "/manager")
public class ManagerControl {
	@Resource
	IManagerFun ManagerService;
	@Resource
	private ICheckLogin loginService;

	@RequestMapping(value = "addOp.action")
	public String addOp() {
		return "manager/addOpeartor";
	}

	@RequestMapping(value = "insertOp.action")
	public String insertOp(HttpSession session, EleOpeartor op) {
		boolean result = ManagerService.insertOp(op);
		if (result) {
			session.setAttribute("addopeartor", "添加成功");
		} else {
			session.setAttribute("addopeartor", "添加失败");
		}
		return "manager/addOpeartor";
	}

	@RequestMapping(value = "releaseInfo.action")
	public String releaseInfo() {
		return "manager/releaseInfo";
	}

	@RequestMapping(value = "publishInfo.action")
	public String publishInfo(HttpSession session, EleInfo info) {
		boolean result = ManagerService.insertInfo(info);
		if (result) {
			session.setAttribute("publishInfo", "发布成功");
			EleInfo newinfo = loginService.selectInfo();
			session.setAttribute("newInfo", newinfo);
		} else {
			session.setAttribute("publishInfo", "发布失败");
		}
		return "manager/releaseInfo";
	}

	@RequestMapping(value = "allInfo.action")
	public String allInfo(HttpSession session) {
		List allinfo = ManagerService.selectAllInfo();
		session.setAttribute("allInfo", allinfo);
		return "manager/publishInfo";
	}

	@RequestMapping(value = "allMind.action")
	public String allMind(HttpSession session) {
		// 跳转到最新的界面
		List<EleMind> listMind = ManagerService.selectMind();
		session.setAttribute("listMind", listMind);
		return "manager/mind";
	}

	@RequestMapping(value = "selectAllMind.action")
	public String selectAllMind(HttpSession session) {
		// 查询所有的介意和投诉信息
		List<EleMind> listMind = ManagerService.selectAllMind();
		session.setAttribute("listMind", listMind);
		return "manager/mind";
	}

	@RequestMapping(value = "figure.action")
	public String figure() {
		// 电费数据曲线
		return "manager/MyFigure";
	}

	@ResponseBody
	@RequestMapping(value = "getFigure.action")
	public List getFigure(HttpSession session, String opt) {
		// 电费数据曲线
		List allCoast = ManagerService.selectAllEleCoast();
		System.out.println(opt);
		return allCoast;
	}

	@RequestMapping(value = "changeEle.action")
	public String changeEle(HttpSession session, String change) {
		// 电费的变化
		boolean result = ManagerService.insertChange(change);
		return "manager/MyFigure";
	}

	@RequestMapping(value = "upFile.action")
	public String upFile() {
		// 上传文件的处理
		return "manager/upFile";
	}

	@RequestMapping(value = "upTestFile.action")
	public String upTestFile(@RequestParam("myFile") MultipartFile[] myFiles, HttpServletRequest request) {
		// 上传文件的处理
		// 进行性能计算
		long lStartTime = System.currentTimeMillis();
		for (MultipartFile file : myFiles) {
			// 获取文件 上传的服务器的路径地址
			String strBasePath = request.getSession().getServletContext().getRealPath("myUpFile/file");
			// 新文件名
			String strFileNewName = "\\" + new Date().getTime() + file.getOriginalFilename();
			// 上传到服务器的文件全路径名
			String upFileName = strBasePath + strFileNewName;
			// 创建新上传的文件对象
			
			File upFile = new File(upFileName);
			System.out.println(upFileName);
				try {
					file.transferTo(upFile);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return "manager/upFile";
	}

}
